from tkinter import *
from tkinter.simpledialog import *
import tkinter.font as tkFont
from tool_eventer import Eventer
from tool_draw import *

# 功能一旦解耦，交互就会复杂一层


class NetShow2(Frame):      # 只需负责激活      # page2
    class LampShape(LampShape):
        def __init__(self, canvas, x, y, name):
            LampShape.__init__(self, canvas, x, y, name)

        def white(self):
            self.canvas.itemconfig(self.rectid, outline="white", fill="black")
            if self.textid is not None:
                self.canvas.itemconfig(self.textid, fill="white")

        def gray(self):
            # self.canvas.itemconfig(self.rectid, outline="DarkGray", width=1)
            self.canvas.itemconfig(self.rectid, outline="DarkGray", fill="black")
            if self.textid is not None:
                self.canvas.itemconfig(self.textid, fill="DarkGray")
    
    class ConnectShape(ConnectShape):
        def __init__(self, canvas, startlamp, endlamp, color):
            ConnectShape.__init__(self, canvas, startlamp, endlamp, color)

    def __init__(self, page, master=None):  # page是因为拖拽需要
        Frame.__init__(self, master)
        # NetShow2.LampShape.ft = tkFont.Font(family='Fixdsys', size=20, weight=tkFont.BOLD) # tk初始化后才能工作
        NetShow2.LampShape.ft = tkFont.Font(family='Fixdsys', size=15)
        self.canvas = Canvas(self, width=2000, height=1000, background="gray")
        self.relx, self.rely = 0.5, 0.5
        self.canvas.place(anchor=CENTER, relx=0.5, rely=0.5)
        self.page = page
        
        

        # self.netcontrol = None      # 携带的信息
        self.lampshape_dict = {}
        
        
        self.drag1 = None        
        self.config_event()


        

    
    def config_event(self):
        self.eventer = Eventer(b_canvas=self.canvas.bind, b_tag=self.canvas.tag_bind)
        eventer = self.eventer

        # 设置为激活
        # @eventer.bind("<ButtonRelease-1>", "nothing")   # <ButtonRelease-1> 绑lamp不对，此将指代lamp按下，而非在lamp上释放的ButtonRelease事件
        @eventer.tag_bind("<ButtonRelease-1>", "lamp", "nothing")   # <ButtonRelease-1> 绑lamp不对，此将指代lamp按下，而非在lamp上释放的ButtonRelease事件
        def _(e):
            lampshape = self.findlampshape(e.x, e.y)
            if lampshape:
                self.canvas.itemconfig(lampshape.rectid, fill="red")


        

        # 突出关联
        @eventer.tag_bind("<Enter>", "lamp", "nothing", 66)
        def _(e):
            lampshape = self.findlampshape(e.x, e.y)
            self.stay = lampshape
            lampshape.white()
            for lampshape2 in (connectshape.endlamp for connectshape in lampshape.connectto):
                lampshape2.white()
            for lampshape2 in (connectshape.startlamp for connectshape in lampshape.connectfrom):
                lampshape2.white()
        @eventer.tag_bind("<Leave>", "lamp", "nothing", 66)
        def _(e):
            if self.stay is None: return
            lampshape = self.stay
            lampshape.gray()
            for lampshape2 in (connectshape.endlamp for connectshape in lampshape.connectto):
                lampshape2.gray()
            for lampshape2 in (connectshape.startlamp for connectshape in lampshape.connectfrom):
                lampshape2.gray()
            self.stay = None
        
        # 拖动画布
        @eventer.bind("<Button-1>", "nothing")
        def _(e):
            self.drag1 = e.x, e.y
        @eventer.bind("<B1-Motion>", "nothing")
        def _(e):

            self.drag1 = e.x, e.y

        # 开始幻灯片
        @eventer.bind("<Button-2>", "nothing")
        def _(e):
            self.page.end()
            page3.start()



    def findlampshape(self, x, y):
        cells = self.canvas.find_overlapping(x-2, y-2, x+2, y+2)
        for id in reversed(cells):
            lampshape = self.lampshape_dict.get(id, None)
            if lampshape is not None:
                return lampshape
            
    def input(self, info):
        lamps, connects = info 
        d = {}
        for name, x, y in lamps:
            if name.startswith("__"):
                lampshape = NetShow2.LampShape(self.canvas, x, y, name)
            elif "__" in name:
                lampshape = NetShow2.LampShape(self.canvas, x, y, name.split("__")[0])
            else:
                lampshape = NetShow2.LampShape(self.canvas, x, y, name)
            self.lampshape_dict[lampshape.rectid] = lampshape
            d[name] = lampshape
        for source, targets in connects.items():
            lamp1 = d[source]
            for target in targets:
                lamp2 = d[target]
                connectshape = NetShow2.ConnectShape(self.canvas, lamp1, lamp2, color="green")
                lamp1.connectto.append(connectshape)
                lamp2.connectfrom.append(connectshape)




class Page2:
    def __init__(self, mainwindow):
        self.mainwindow = mainwindow
        self.area = None
        self.mover = None
        self.sb = None
        self.cells = []
        self.scroll = 0 # 偏移量
        

    def start(self):
        self.area = Frame(self.mainwindow)
        self.area.pack(fill=BOTH, expand=1)
        self.mover = Frame(self.area)
        self.mover.place(anchor=NW, relwidth=1.4, relheight=1, x=0, y=0)
        self.sb = Scrollbar(self.mainwindow, orient=HORIZONTAL)
        self.sb.pack(side=BOTTOM, fill=X)
        self.sb.set("0", "0.1")
        def _(*a):  # 三种滚动事件
            if a[0] == "moveto":
                num = a[1]
                # self.sb.set(num, str(float(num)+0.01))
                self.sb.set(num, str(float(num)+0.1))
                self.scroll = float(num)*800
                self.mover.place(anchor=NW, x=0-self.scroll, y=0, relwidth=1.4, relheight=1)
            else:
                return
            # elif a[0] == "scroll":
            #     if a[2] == "units":
            #         pass
            #     else:   # "pages"
            #         pass
        self.sb.config(command=_)
        self.mover.rowconfigure(0, weight=1)
        self.mover.columnconfigure(0, weight=1) # place没有pad，所以用了grid
        self.mover.columnconfigure(1, weight=1)
        self.mover.columnconfigure(2, weight=1)
        self.mover.columnconfigure(3, weight=1)
        self.cells.append(NetShow2(self.mover))
        self.cells.append(NetShow2(self.mover))
        self.cells.append(NetShow2(self.mover))
        self.cells.append(NetShow2(self.mover))
        self.cells[0].grid(row=0, column=0, padx=1, sticky=N+E+W+S)
        self.cells[1].grid(row=0, column=1, padx=1, sticky=N+E+W+S)
        self.cells[2].grid(row=0, column=2, padx=1, sticky=N+E+W+S)
        self.cells[3].grid(row=0, column=3, padx=1, sticky=N+E+W+S)
        
        self.cells[0].input(self.mainwindow.mode_dict[1].netshow.output())                                     # 尼玛，page间共享数据。
        self.cells[1].input(self.mainwindow.mode_dict[1].netshow.output())
        self.cells[2].input(self.mainwindow.mode_dict[1].netshow.output())
        self.cells[3].input(self.mainwindow.mode_dict[1].netshow.output())


    def end(self):
        self.area.destroy()     # 其子部件也会被销毁
        self.area = None
        self.cells.clear()
        self.sb.destroy()
        self.sb = None
        


